package com.bytedance.frameworks.baselib.network.connectionclass;

import com.bytedance.common.utility.Logger;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicReference;

public final class ConnectionClassManager {
    public static final class Holder {
        public static final ConnectionClassManager sInstance;

        static {
            sInstance = new ConnectionClassManager((byte) 0);
        }
    }

    public interface b {
        void a(ConnectionQuality arg1);
    }

    public AtomicReference<ConnectionQuality> a;
    public ArrayList<ConnectionClassManager.b> b;
    private d c;
    private volatile boolean d;
    private AtomicReference<ConnectionQuality> e;
    private int f;

    private ConnectionClassManager() {
        this.c = new d();
        this.d = false;
        this.a = new AtomicReference(ConnectionQuality.UNKNOWN);
        this.b = new ArrayList();
    }

    ConnectionClassManager(byte arg1) {
        this();
    }

    public final synchronized ConnectionQuality a() {
        ConnectionQuality v0_1;
        if (this.c != null) {
            double v0_2 = this.c.b;
            if (v0_2 < 0) {
                v0_1 = ConnectionQuality.UNKNOWN;
                return v0_1;
            }

            if (v0_2 < 28) {
                v0_1 = ConnectionQuality.POOR;
                return v0_1;
            }

            if (v0_2 < 112) {
                v0_1 = ConnectionQuality.MODERATE;
                return v0_1;
            }

            if (v0_2 < 560) {
                v0_1 = ConnectionQuality.GOOD;
                return v0_1;
            }

            v0_1 = ConnectionQuality.EXCELLENT;
            return v0_1;
        }

        v0_1 = ConnectionQuality.UNKNOWN;
        return v0_1;
    }

    public final synchronized void calculate(long bytes, long timeInMs) {
        double v8 = 1;
        double v6 = 0.95;
        double v0 = (((double) bytes)) / (((double) timeInMs)) * 8;
        if (timeInMs != 0 && v0 >= 3) {
            if (Logger.debug()) {
                Logger.d("ConnectionClassManager", "bytes = " + bytes + " timeInMs = " + timeInMs + " bandwidth = " + v0);
            }

            d v2 = this.c;
            if (v2.c > v2.a) {
                v2.b = Math.exp(Math.log(v0) * 0.05 + Math.log(v2.b) * v6);
            } else if (v2.c > 0) {
                double v4 = (((double) v2.c)) * v6 / ((((double) v2.c)) + v8);
                v2.b = Math.exp(Math.log(v0) * (v8 - v4) + v4 * Math.log(v2.b));
            } else {
                v2.b = v0;
            }

            ++v2.c;
            if (this.d) {
                ++this.f;
                if (this.a() != this.e.get()) {
                    this.d = false;
                    this.f = 1;
                }

                if ((((double) this.f)) < 5) {
                    return;
                }

                if (!this.b()) {
                    return;
                }

                this.d = false;
                this.f = 1;
                this.a.set(this.e.get());
                this.c();
                return;
            }

            if (this.a.get() == this.a()) {
                return;
            }

            this.d = true;
            this.e = new AtomicReference(this.a());
            return;
        }
    }

    private boolean b() {
        boolean v0;
        double v8 = 560;
        double v6 = 112;
        double v4 = 28;
        if (this.c == null) {
            v0 = false;
            return v0;
        }

        try {
            switch (com.bytedance.frameworks.baselib.network.connectionclass.b.a[this.a.get().ordinal()]) {
                case 1: {
                    v6 = 0;
                    break;
                }
                case 2: {
                    double v10 = v6;
                    v6 = v4;
                    v4 = v10;
                    break;
                }
                case 3: {
                    v4 = v8;
                    break;
                }
                case 4: {
                    v4 = 3.4028234663852886E38d;
                    v6 = v8;
                    break;
                }
            }
        } catch (Throwable v0_1) {
            v0_1.printStackTrace();
        }

        try {
            label_18:
            v8 = this.c.b;
            if (v8 <= v4) {
                if (v8 < 0.8 * v6) {
                    v0 = true;
                } else {
                    return false;
                }
                return v0;
            }
        } catch (Throwable v0_1) {
            label_41:
            v0_1.printStackTrace();
            return false;
        }

        if (v8 <= v4 * 1.25) {
            return false;
        }
        return true;

    }

    private void c() {
        try {
            int v3 = this.b.size();
            int v2;
            for (v2 = 0; v2 < v3; ++v2) {
                this.b.get(v2).a(this.a.get());
            }
        } catch (Throwable v0) {
            v0.printStackTrace();
        }
    }
}

